package myservice.android.activities;

import myservice.android.Global;
import myservice.android.R;
import myservice.android.database.Database;
import myservice.android.utilities.EditTextMonitor;
import myservice.android.utilities.SpinnerMonitor;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.database.Cursor;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;

public class PublicationActivity extends BaseSaveableActivity {	
	
	private EditText titleText;
	private Spinner publicationTypeSpinner;
	private Button deleteButton, saveButton;
	
	private long publicationId;		
	
	@Override
	protected void initializeLayoutIdAndTitle() {
		layoutId = R.layout.publication;
		activityTitle = Global.res.getString(R.string.title_publication_properties);		
	}

	@Override
	protected void retrieveViews() {
        titleText = (EditText) findViewById(R.id.publicationTitleText);
        publicationTypeSpinner = (Spinner) findViewById(R.id.publicationTypeSpinner);
        saveButton = (Button) findViewById(R.id.saveButton);
        deleteButton = (Button) findViewById(R.id.deleteButton);
	}

	@Override
	protected void loadActivityParameters() {
		publicationId = activityParameters.getLong("publication_id");		
	}

	@Override
	protected void assignListeners() {
		
		titleText.addTextChangedListener(new EditTextMonitor(this));
        publicationTypeSpinner.setOnItemSelectedListener(new SpinnerMonitor(this));        
		
        deleteButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(PublicationActivity.this);
				alertDialogBuilder.setTitle(Global.res.getText(R.string.title_warning));				
				alertDialogBuilder
					.setMessage(Global.res.getText(R.string.message_delete_publication_confirmation))
					.setPositiveButton(Global.res.getText(R.string.button_yes),new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog,int id) {
							Database.deletePublication(publicationId);
							finish();
						}
					  })
					.setNegativeButton(Global.res.getText(R.string.button_no),new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog, int id) {
							dialog.cancel();
						}
					});
		 
				AlertDialog alertDialog = alertDialogBuilder.create();
				alertDialog.show();				
			}        	
        });   
        
        saveButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				save();
				finish();
			}
        });   
          
	}

	@Override
	protected void updateUi() {
   		Cursor cursor = Database.select("SELECT title,type FROM publications WHERE publicationid="+publicationId);

       	if (cursor.moveToNext()==true) {
       		titleText.setText(cursor.getString(cursor.getColumnIndex("title")));
       		publicationTypeSpinner.setSelection(cursor.getInt(cursor.getColumnIndex("type"))); 
       	}        

        cursor.close();
       	
	}	
	  
    
    @Override
    protected void save() {    	
		Database.execSQL("UPDATE publications SET title='"+Global.fixText(titleText.getText())+"', type="+publicationTypeSpinner.getSelectedItemId()+
				         ", timestamp="+Global.getTimeStamp()+" WHERE publicationid="+publicationId);		   	
    }
    
}